package com.mzy.linear_struct.subject;

import java.awt.image.Kernel;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Author: codermzy
 * @Date: 2024/04/22/下午3:36
 * @Description:
 */
public class 数组中的第K个最大元素215 {

    static class Solution {
        public int findKthLargest(int[] nums, int k) {
            if (nums.length < 1 || k <= 0) return -1;

            PriorityQueue<Integer> priority = new PriorityQueue<>((a, b) -> b - a);
            for (int num : nums) {
                priority.offer(num);
            }
            int answer = priority.poll();
            for (int i = 0; i < k - 1; i++) {
                answer = priority.poll();
            }
            return answer;
        }
    }

    public static void main(String[] args) {
        int[] nums = {3, 2, 1, 5, 6, 4};
        System.out.println(new Solution().findKthLargest(nums, 2));
    }
}
